const fp = require('loadsh/fp');
const { MayBe, Container } = require('./support');

// 使用fp.add(x,y)和fp.map(x,y)创建能让functor里的值增加的函数ex1
let mayBe = MayBe.of([5, 6, 1]);
let ex1 = (value) => {
    return fp.map(fp.add(value), mayBe._value);
}
console.log(ex1(1));
// 使用fp.first获取第一个元素
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do']);
let ex2 = () => {
    return fp.first(xs._value)
}
console.log(ex2());
// 使用 safeProp 和 fp.first 找到user的名字的首字母
let safeProp = fp.curry(function (x, o) {
    return MayBe.of(o[x])
})
let user = { id: 2, name: 'Albert' }
let ex3 = () => {
    let { _value } = safeProp("name", user)
    return fp.first(_value)
}
console.log(ex3());
// 使用MayBe重写ex4 不带if语句
let ex4 = (n) => {
    return MayBe.of(n).map(parseInt)._value
}
console.log(ex4(1));